<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-10 04:14:17
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-27 22:58:52
 */


namespace common\addons\diandi_shop\api;

use Yii;
use api\controllers\AController;
use common\addons\diandi_shop\services\GoodsService;
use common\helpers\ResultHelper;

/**
 * Class GoodsController
 */
class GoodsController extends AController
{
    public $modelClass = '\common\models\DdGoods';
    protected $authOptional = ['*'];

    /**
     * @SWG\Get(path="/diandi_shop/goods/search",
     *     tags={"商品"},
     *     summary="商品检索",
     *     @SWG\Response(
     *          response = 200,
     *          description = "关键词商品检索",
     *     ),
     *     @SWG\Parameter(
     *          in="query",
     *          name="page",
     *          type="integer",
     *          description="分页",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
     *          in="query",
     *          name="pageSize",
     *          type="integer",
     *          description="显示条数",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
     *          in="query",
     *          name="keywords",
     *          type="string",
     *          description="关键词",
     *          required=false,
     *   ),
     * )
     */
    public function actionSearch()
    {
        $keytwords = Yii::$app->request->get('keywords');
        $pageSize = Yii::$app->request->get('pageSize');
        $user_id = Yii::$app->user->identity->member_id;

        $list = GoodsService::getList(0, 0, $keytwords, $pageSize, $user_id);
        return ResultHelper::json(200, '获取成功', $list);
    }
    /**
     * @SWG\Get(path="/diandi_shop/goods/detail",
     *     tags={"商品"},
     *     summary="商品详情",
     *     @SWG\Response(
     *         response = 200,
     *         description = "商品详情",
     *     ),
     *     @SWG\Parameter(
     *        in="query",
     *        name="access-token",
     *        type="string",
     *        description="用户秘钥",
     *        required=true,
     *   ),
     *     @SWG\Parameter(
     *            in="query",
     *            name="goods_id",
     *            type="integer",
     *            description="商品id",
     *            required=true,
     *   ),
     * )
     */
    public function actionDetail()
    {
        $goods_id = Yii::$app->request->get('goods_id');
        $goods = GoodsService::getDetail($goods_id);
        return ResultHelper::json(200, '获取成功', $goods);
    }

    /**
     * @SWG\Get(path="/diandi_shop/goods/lists",
     *     tags={"商品"},
     *     summary="Retrieves the collection of Goods resources.",
     *     @SWG\Response(
     *          response = 200,
     *          description = "Goods collection response",
     *     ),
     *     @SWG\Parameter(
     *          in="query",
     *          name="page",
     *          type="string",
     *          description="分页",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
     *          in="query",
     *          name="pageSize",
     *          type="string",
     *          description="显示条数",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
     *          in="query",
     *          name="category_pid",
     *          type="integer",
     *          description="父级分类id",
     *          required=false,
     *   ),
     *   @SWG\Parameter(
     *          in="query",
     *          name="category_id",
     *          type="integer",
     *          description="分类id",
     *          required=false,
     *   ),
     * )
     */
    public function actionLists()
    {
        $data = Yii::$app->request->get();
        $pageSize = Yii::$app->request->get('pageSize');
        $user_id = Yii::$app->user->identity->member_id;
        $list = GoodsService::getList($data['category_pid'], $data['category_id'], '', $pageSize, $user_id);
        return ResultHelper::json(200, '获取成功', $list);
    }
}
